<template>
  <div class="warp">
    <div class="film-info">
      <div class="film-info__title">
        {{ film.name }}<span>{{ film.grade }}分</span>
      </div>
      <div class="film-info__item">
        {{ film.category }} | {{ film.runtime }}分钟
      </div>

      <div class="film-info__more"></div>
    </div>

    <div class="body">
      <!-- <van-tabs :active="date" sticky shrink @change="emit('change', $event)">
        <van-tab
          v-for="(item, index) in displayShowDate"
          :key="index"
          :name="item.time"
          :title="item.label"
        >
          <van-cell v-for="item in list" :key="item.scheduleId">
            {{ item.filmLanguage }} - {{ item.salePrice }}
          </van-cell>
        </van-tab>
      </van-tabs> -->

      <div class="tabs">
        <div
          ref="itemRef"
          class="tabs__item"
          v-for="(item, index) in displayShowDate"
          :key="index"
          @click="emit('change', item.time)"
        >
          {{ item.label }}
        </div>

        <!-- 高亮的线条 -->
        <div class="tabs__line" :style="lineStyle"></div>
      </div>

      <van-cell v-for="item in list" :key="item.scheduleId">
        {{ item.filmLanguage }} - {{ item.salePrice }}
      </van-cell>
    </div>
  </div>
</template>

<script lang="ts" setup>
import * as dayjs from "dayjs";
import { computed } from "vue";
import { getWeekTwoByDate } from "@/utils";
import { ref, onMounted } from "vue";
import { watch } from "vue";

const props = defineProps<{
  // 当前电影
  film: IFilm;
  // 当前电影的场次数据
  list: ISchedule[];
  // 日期
  date: number;
}>();

const emit = defineEmits(["change"]);

// 用于展示的日期
const displayShowDate = computed(() => {
  return props.film.showDate?.map((date) => {
    const label = `${getWeekTwoByDate(date * 1000)}${dayjs(date * 1000).format(
      "MM月DD日"
    )}`;
    return {
      time: date,
      label: label,
    };
  });
});

const itemRef = ref<HTMLElement[] | null>(null);
// 一个 TabItem 元素的宽度
const itemWidth = ref(0);
// 激活的下标
const lineIndex = computed(
  () =>
    displayShowDate.value?.findIndex((item) => item.time === props.date) || 0
);
const lineStyle = computed(() => {
  const left = lineIndex.value * itemWidth.value + itemWidth.value / 2;

  return {
    transform: `translateX(${left}px) translateX(-50%)`,
  };
});

watch(
  displayShowDate,
  () => {
    // 要重新计算下一个 itemWidth
    itemWidth.value = itemRef.value![0].clientWidth;
  },
  {
    flush: "post",
  }
);

onMounted(() => {
  console.log(itemRef.value);
  itemWidth.value = itemRef.value![0].clientWidth;
});
</script>

<style lang="scss" scoped>
.warp {
  position: relative;
  background-color: #fff;
  &::before {
    position: absolute;
    top: -20px;
    left: 50%;
    margin-left: -10px;
    display: block;
    content: "";
    width: 0px;
    height: 0px;
    border-width: 10px;
    border-style: solid;
    border-color: transparent transparent red transparent;
  }

  .film-info {
    position: relative;
    display: flex;
    flex-direction: column;
    text-align: center;
    padding: 15px 0;
    box-sizing: border-box;

    &__title {
      margin-bottom: 5px;
      font-size: 16px;
      color: #000;

      span {
        color: var(--van-primary-color);
      }
    }

    &__item {
      font-size: 14px;
      color: #797d82;
    }

    &__more {
      position: absolute;
      top: 50%;
      right: 15px;
      transform: translateY(-50%);
      width: 5px;
      height: 5px;
      background-color: red;
    }
  }
}

.tabs {
  position: relative;
  display: flex;
  line-height: 48px;
  overflow-x: auto;

  &__item {
    // flex: 1;
    flex: none;
    padding: 0 10px;
    text-align: center;
  }

  &__line {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 40px;
    height: 3px;
    border-radius: 3px;
    background-color: var(--van-primary-color);
    transition: transform 0.3s ease;
  }
}
</style>
